package com.sunchen.asc.application.rest;

import com.sunchen.asc.service.common.ResultResponse;
import com.sunchen.asc.service.item.api.ItemService;
import com.sunchen.asc.service.item.vo.AllItemVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/item")
public class ItemRest {

    @DubboReference
    private ItemService itemService;


    @ApiOperation(value = "创建事项")
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public ResultResponse create(@RequestBody @ApiParam(name="事项下全部vo",value="传入json格式",required=true) AllItemVo allItemVo){

        return itemService.create(allItemVo);
    }

    @ApiOperation(value = "修改事项")
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public ResultResponse edit(@RequestBody @ApiParam(name="事项下全部vo",value="传入json格式",required=true) AllItemVo allItemVo){

        return itemService.edit(allItemVo);
    }

    @ApiOperation(value = "删除事项")
    @ApiImplicitParam(name="id",value = "事项id",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultResponse delete(@RequestParam Long id){

        return itemService.delete(id);
    }

    @ApiOperation(value = "事项详情")
    @ApiImplicitParam(name="id",value = "事项id",required = true)
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ResultResponse detail(@RequestParam Long id){

        return itemService.detail(id);
    }

    @ApiOperation(value = "事项发布")
    @ApiImplicitParam(name="id",value = "事项id",required = true)
    @RequestMapping(value = "/release",method = RequestMethod.GET)
    public ResultResponse release(@RequestParam Long id){

        return itemService.release(id);
    }

    @ApiOperation(value = "事项取消发布")
    @ApiImplicitParam(name="id",value = "事项id",required = true)
    @RequestMapping(value = "/release/cancel",method = RequestMethod.GET)
    public ResultResponse releaseCancel(@RequestParam Long id){

        return itemService.releaseCancel(id);
    }

    @ApiOperation(value = "父级事项详情")
    @ApiImplicitParam(name="id",value = "事项id",required = true)
    @RequestMapping(value = "/father/detail",method = RequestMethod.GET)
    public ResultResponse fatherDetail(@RequestParam Long id){

        return itemService.fatherDetail(id);
    }

    @ApiOperation(value = "事项查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "搜索框",required = false),
            @ApiImplicitParam(name="titleId",value = "主题id",required = false),
            @ApiImplicitParam(name="addressId",value = "地点id",required = false),
            @ApiImplicitParam(name="acceptDeptId",value = "部门id",required = false),
            @ApiImplicitParam(name="itemType",value = "事项类型",required = false),
            @ApiImplicitParam(name="onlineFlag",value = "能否网上办理(0否，1是)",required = false),
            @ApiImplicitParam(name="serviceObject",value = "人群分类",required = false),
            @ApiImplicitParam(name="lifeCycle",value = "生命周期",required = false),
            @ApiImplicitParam(name="titleType",value = "主题类型",required = true),
            @ApiImplicitParam(name="page",value = "页码",required = false,defaultValue = "1"),
            @ApiImplicitParam(name="limit",value = "每页显示条数",required = false,defaultValue = "10"),
            @ApiImplicitParam(name="order",value = "排序字段",required = false,defaultValue = "id"),
            @ApiImplicitParam(name="orderType",value = "升序/降序",required = false,defaultValue = "asc")
    })
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultResponse list(@RequestParam(value = "name",required = false) String name,
                               @RequestParam(value = "titleId",required = false) Long titleId,
                               @RequestParam(value = "addressId",required = false) Long addressId,
                               @RequestParam(value = "acceptDeptId",required = false) Long acceptDeptId,
                               @RequestParam(value = "itemType",required = false) String itemType,
                               @RequestParam(value = "onlineFlag",required = false) Integer onlineFlag,
                               @RequestParam(value = "serviceObject",required = false) String serviceObject,
                               @RequestParam(value = "lifeCycle",required = false) String lifeCycle,
                               @RequestParam Integer titleType,
                               @RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                               @RequestParam(value = "limit",defaultValue = "10",required = false) Integer limit,
                               @RequestParam(value = "order",defaultValue = "id",required = false) String order,
                               @RequestParam(value = "orderType",defaultValue = "asc",required = false) String orderType){

        Sort sort = Sort.by(Sort.Direction.fromString(orderType),order);
        Pageable pageable = PageRequest.of(page-1,limit,sort);

        return itemService.list(name,titleId,addressId,acceptDeptId,itemType,onlineFlag,serviceObject,lifeCycle,titleType,pageable);
    }

    @ApiOperation(value = "更改事项标记")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "事项id",required = true),
            @ApiImplicitParam(name="itemMark",value = "标记状态",required = true)
    })
    @RequestMapping(value = "/mark",method = RequestMethod.GET)
    public ResultResponse mark(@RequestParam Long id,@RequestParam Integer itemMark){

        return itemService.mark(id,itemMark);
    }

    @ApiOperation(value = "获取子事项")
    @ApiImplicitParam(name="id",value = "事项id",required = true)
    @RequestMapping(value = "/son/list",method = RequestMethod.GET)
    public ResultResponse sonList(@RequestParam Long id){

        return itemService.sonList(id);
    }
}
